package com.xueyu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xueyu.dao.DrugDao;
import com.xueyu.dao.OrdDao;
import com.xueyu.util.ResultBean;
import com.xueyu.domain.drug.drug;
import com.xueyu.domain.goods.OrderRequest;
import com.xueyu.domain.goods.Orderapplydetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/Ord")
public class OrdContoller {
    @Autowired
    OrdDao OrdDao;
    @Autowired
    DrugDao DrugDao;

    @RequestMapping("/get")
    public ResultBean get(@RequestBody OrderRequest request) {
        Orderapplydetail Ord = new Orderapplydetail();
        List<Map<String, String>> ordList = request.getOrd();

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String currentDate = dateFormat.format(new Date());
        Random random = new Random();
        int randomNum = random.nextInt(900000000) + 1000000000; // 生成10位随机数
        String orderNumber = currentDate + randomNum; //订单号
        for (Map<String, String> ord : ordList) {
            String drug = ord.get("drug");
            Ord.setProdid(Integer.valueOf(drug));
            String quantity = ord.get("quantity");
            Ord.setOrderdetailid(0);//订单id
            Ord.setOrd(0);
            Ord.setOrderno(orderNumber);//订单号
            QueryWrapper<drug> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("DrugID", drug);
            List<drug> page = DrugDao.selectList(queryWrapper);
            if (page != null) {
                drug firstDrug = page.get(0);
                firstDrug.getStyle();//商品单位
                firstDrug.getPrice();//商品单价
                Ord.setUnitname( firstDrug.getStyle());//订单的商品单位
                Ord.setPrice((double) firstDrug.getPrice());//订单的商品单位
                Ord.setProdcount(Integer.parseInt(quantity));//数量
                double result =  firstDrug.getPrice() * Integer.parseInt(quantity);
                Ord.setTotalprice(result);//小计
                Date date = new Date();
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String currentTime = sdf1.format(date);
                Ord.setTime(currentTime);
                OrdDao.insert(Ord);
            }
        }
        return new ResultBean();
    }
}

